<template>
  <el-dialog
    :visible.sync="visible"
    :title="title"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    top="3%"
    :width="size"
  >
    <!-- <el-tabs v-model="activeName" v-if="dataForm.goodsType == 2">
      <el-tab-pane label="基本信息" name="first"></el-tab-pane>
      <el-tab-pane label="购买须知" name="second"></el-tab-pane>
    </el-tabs> -->
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      :label-width="$i18n.locale === 'en-US' ? '100px' : '100px'"
      :disabled="disabled"
    >
      <el-row>
        <el-col :span="12" v-if="dataForm.goodsType == 2">
          <el-form-item label="商品名称" prop="goodsName">
            <el-input
              v-model="dataForm.goodsName"
              placeholder="商品名称"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24" v-if="dataForm.goodsType == 3">
          <el-form-item label="成团人数" prop="clusteringNum">
            <el-input
              v-model="dataForm.clusteringNum"
              placeholder="成团人数"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="dataForm.goodsType == 2">
          <el-form-item label="座位数" prop="seatNum">
            <el-input
               type="number"
              min="0"
              v-model="dataForm.seatNum"
              placeholder="座位数"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24" v-if="dataForm.goodsType == 1">
          <el-form-item label="数量" prop="initNum">
            <el-select
              v-model="dataForm.initNum"
              placeholder="请选择数量"
              clearable
              style="width: 100%"
            >
              <el-option
                :label="item"
                :value="item"
                v-for="(item, index) in nums"
                :key="index"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="dataForm.goodsType == 2">
          <el-form-item label="价格" prop="normalPrice">
            <el-input
              type="number"
              v-model="dataForm.normalPrice"
              placeholder="价格"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24" v-if="dataForm.goodsType == 1">
          <el-form-item label="优惠价" prop="discountPrice">
            <el-input
              type="number"
              v-model="dataForm.discountPrice"
              placeholder="优惠价"
              min="0"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24" v-if="dataForm.goodsType == 1">
          <el-form-item label="开抢时间" prop="robTime">
            <el-time-picker
              v-model="dataForm.robTime"
              value-format="HH:mm"
              :default-value="dataForm.robTime"
              format="HH:mm"
              placeholder="开抢时间"
              style="width: 100%"
              :disabled="dataForm.goodsType == 1 ? true : false"
            >
            </el-time-picker>
          </el-form-item>
        </el-col>
        <template v-if="dataForm.goodsType != 2">
          <el-col :span="24">
            <el-form-item label="游玩日期" prop="playDate">
              <el-date-picker
                v-model="dataForm.playDate"
                type="date"
                :default-value="dataForm.playDate"
                value-format="yyyy-MM-dd"
                placeholder="选择日期"
                style="width: 100%"
                :disabled="dataForm.goodsType == 1 ? true : false"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="游玩时间" prop="playTime">
              <el-select
                v-model="dataForm.playTime"
                placeholder="请选择数量"
                clearable
                style="width: 100%"
              >
                <el-option
                  :label="item"
                  :value="item"
                  v-for="(item, index) in times"
                  :key="index"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </template>
        <el-col :span="12" v-if="dataForm.goodsType == 2">
          <el-form-item label="特色标签" prop="featureLabels">
            <el-input
              v-model="dataForm.featureLabels"
              placeholder="特色标签"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="dataForm.goodsType == 2">
          <el-form-item label="活动区域" prop="playArea">
            <el-input
              v-model="dataForm.playArea"
              placeholder="活动区域"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="dataForm.goodsType == 2">
          <el-form-item label="游玩时长" prop="playLength">
            <el-input
              type="number"
              min="0"
              v-model="dataForm.playLength"
              placeholder="游玩时长"
            ></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12" v-if="dataForm.goodsType == 2">
          <el-form-item label="作业方式" prop="workWay">
            <el-input
              v-model="dataForm.workWay"
              placeholder="作业方式"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="dataForm.goodsType == 2">
          <el-form-item label="上船地点" prop="boardSite">
            <el-input
              v-model="dataForm.boardSite"
              placeholder="上船地点"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24" v-if="dataForm.goodsType == 2">
          <el-form-item label="上下架" prop="status">
            <el-radio-group v-model="dataForm.status">
              <el-radio :label="1">上架</el-radio>
              <el-radio :label="0">下架</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="dataForm.goodsType == 2">
          <p class="pdl60">
            <span style="color: #f56c6c; margin-right: 4px">*</span
            >商品封面<span>建议(建议尺寸220*256px)</span>
          </p>
        </el-col>
        <el-col :span="12" v-if="dataForm.goodsType == 2">
          <p class="pdl60">行程亮点<span>(建议宽度750px)</span></p>
        </el-col>
        <el-col :span="12">
          <el-form-item label="" prop="coverImg">
            <el-upload
              class="avatar-uploader"
              :action="url"
              :show-file-list="false"
              :on-success="
                (res, file, fileList) => successHandle(res, file, fileList)
              "
              :before-upload="(file) => beforeUploadHandle(file, 'coverImg')"
            >
              <img
                v-if="dataForm.coverImg"
                :src="dataForm.coverImg"
                class="avatar"
              />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="" prop="tripLights">
            <el-upload
              class="avatar-uploader"
              :action="url"
              :show-file-list="false"
              :on-success="
                (res, file, fileList) => successHandle1(res, file, fileList, 1)
              "
              :before-upload="(file) => beforeUploadHandle(file, 'tripLights',true)"
            >
              <img
                v-if="dataForm.tripLights"
                :src="dataForm.tripLights"
                class="avatar"
              />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="购买须知" prop="buyGuide">
            <editor-vue
              style="margin-top: -30px"
              :content="dataForm.buyGuide"
              @EditorChange="getEditor"
              ref="buyGuideRef"
            ></editor-vue>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template slot="footer" v-if="!disabled">
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{
        $t("confirm")
      }}</el-button>
    </template>
    <!-- 裁剪框 -->
    <cropper-dlg
      v-if="showCropper"
      :dialogVisible.sync="showCropper"
      :img-file="currentFile"
      ref="vueCropper"
      @upload="cropperUpload"
    ></cropper-dlg>
  </el-dialog>
</template>

<script>
import Cookies from "js-cookie";
import debounce from "lodash/debounce";
import editorVue from "@/components/editor";
export default {
  data() {
    return {
      currentFile: "",
      cropperIndex: null,
      showCropper: false, //裁剪弹框
      isCropper: true, //上传之前是否显示裁剪
      disabled: false,
      url: `${
        window.SITE_CONFIG["apiURL"]
      }/oss/file/aliyunUpload?access_token=${Cookies.get("access_token")}`,
      url2: `${
        window.SITE_CONFIG["apiURL"]
      }/oss/file/tinymce/upload?access_token=${Cookies.get("access_token")}`,
      fileList: [],
      Scenicspotdata: [],
      scenicOptions: [],
      replaceList: [],
      replaceList2: [],
      fileList2: [], //轮播图
      fileList1: [], //封面图
      visible: false,
      dialogVisible1: false,
      dialogImageUrl1: "",
      dialogImageUrl2: "",
      dialogVisible2: false,
      activeName: "first",
      size: "30%",
      title: "",
      nums: [1, 2, 3, 4, 5, 6, 7, 8, 9],
      times: [
        "05:00",
        "06:00",
        "07:00",
        "08:00",
        "09:00",
        "10:00",
        "11:00",
        "12:00",
        "13:00",
        "14:00",
        "15:00",
        "16:00",
      ],
      dataForm: {
        id: "",
        boardSite: "",
        initNum: null,
        residueNum: null,
        status: "",
        buyGuide: "",
        playDate: "",
        playTime: "",
        clusteringNum: "",
        discountPrice: "",
        featureLabels: "",
        goodsName: "",
        goodsType: "",
        merchantId:
          this.$store.state.user.MerchantId ||
          localStorage.getItem("MerchantId"),
        normalPrice: "",
        playArea: "",
        playLength: "",
        publishType: 0,
        robTime: "20:00",
        seatNum: "",
        coverImg: "",
        tripLights: "",
        workWay: "",
      },
    };
  },
  props: ["goodsType"],
  created() {},
  components: {
    editorVue,
  },
  watch: {
    activeName(newval, oldval) {
      if (newval == "second") {
        this.$nextTick(() => {
          this.$refs.buyGuideRef.init();
        });
      }
    },
  },
  computed: {
    dataRule() {
      return {
        goodsName: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        buyGuide: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        playLength: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        initNum: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        boatType: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        coverImg: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        clusteringNum: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        seatNum: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        normalPrice: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        status: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        status: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        playArea: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        workWay: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        boardSite: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        playDate: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        playTime: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        discountPrice: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        initNum: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        robTime: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        // tripLights: [
        //   {
        //     required: true,
        //     message: this.$t("validate.required"),
        //     trigger: "blur",
        //   }
        // ]
      };
    },
  },
  methods: {
    getEditor(val) {
      this.dataForm.buyGuide = val;
    },
    /**
     *上传文件限制--只能上传图片格式的文件
     */
    // 上传之前
    beforeUploadHandle(file, name,notCropper) {
      if (
        file.type !== "image/jpg" &&
        file.type !== "image/jpeg" &&
        file.type !== "image/png" &&
        file.type !== "image/gif"
      ) {
        this.$message.error(
          this.$t("upload.tip", {
            format: "jpg、png、gif",
          })
        );
        return false;
      }
      if(notCropper){
        this.isCropper=false;
      }else{
        this.isCropper=true;
      }
      if (this.isCropper) {
        //显示裁剪
        this.currentFile = file;
        this.cropperIndex = name;
        this.showCropper = true;
        this.$nextTick(() => {
          this.$refs.vueCropper.init();
        });
        return false;
      }
    },
    cropperUpload(data) {
      this.showCropper = false;
      if (data != "close") {
        //裁剪成功后获取的图片
        this.$http.post("/oss/file/aliyunUpload", data).then((res) => {
          if (res.data.code == 0) {
            if (this.cropperIndex == "tripLights") {
              //门头照
              this.successHandle1(res.data);
            } else {
              //其他照片
              this.successHandle(res.data);
            }
          }
        });
      }
    },
    // 上传成功
    successHandle1(res, file, fileList, index) {
      if (res.code !== 0) {
        return this.$message.error(res.msg);
      }
      this.dataForm.tripLights = res.data.url;
    },
    // 上传成功
    successHandle(res, file, fileList) {
      if (res.code !== 0) {
        return this.$message.error(res.msg);
      }
      this.dataForm.coverImg = res.data.url;
    },
    init() {
      this.visible = true;
      this.dataForm.goodsType = this.goodsType;

      if (this.goodsType != 2) {
        this.size = "30%";
      } else {
        this.size = "60%";
      }
      if (this.dataForm.id) {
        if (this.goodsType == 2) {
          this.title = "编辑";
        }
        if (this.goodsType == 1) {
          this.title = "发布特价";
        }
        if (this.goodsType == 3) {
          this.title = "发布拼船";
        }
      } else {
        this.title = "新增";
      }
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.getInfo();
        } else {
          this.$nextTick(() => {
            this.$refs.buyGuideRef.init();
          });
        }
      });
    },
    GetDateStr(AddCount) {
      var dd = new Date();
      dd.setDate(dd.getDate() + AddCount); //获取AddDayCount天后的日期
      var y = dd.getFullYear();
      var m = dd.getMonth() + 1; //获取当前月份的日期
      var d = dd.getDate();
      m = m > 9 ? m : "0" + m;
      d = d > 9 ? d : "0" + d;
      return y + "-" + m + "-" + d;
    },
    getInfo() {
      this.$http
        .get("/scenic/goods/" + this.dataForm.id)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data,
          };
          if (this.goodsType) {
            this.dataForm.goodsType = this.goodsType;
          }
          if (this.dataForm.goodsType == "1") {
            this.dataForm.playDate = this.GetDateStr(2);
            this.dataForm.robTime = "20:00";
          }
          let fileList1 = this.dataForm.tripLights.split(",");
          fileList1.filter((item) => {
            this.fileList1.push({
              url: item,
            });
          });
          this.$nextTick(() => {
            this.$refs.buyGuideRef.init();
          });
        })
        .catch(() => {});
    },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function () {
        this.$refs["dataForm"].validate((valid) => {
          if (!valid) {
            return false;
          }
          this.dataForm.residueClusteringNum = null;
          if (this.dataForm.goodsType != 2) {
            this.dataForm.id = "";
          }
          // if (this.dataForm.goodsType == 1) {
          //   this.dataForm.residueNum = this.dataForm.initNum;
          // }
          console.log(this.dataForm.id);
          console.log(this.dataForm.goodsType);
          this.$http[
            this.dataForm.id && this.dataForm.goodsType == 2 ? "put" : "post"
          ]("/scenic/goods", this.dataForm)
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.$message({
                message: this.$t("prompt.success"),
                type: "success",
                duration: 500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            })
            .catch(() => {});
        });
      },
      1000,
      {
        leading: true,
        trailing: false,
      }
    ),
  },
};
</script>

<style>
</style><style lang="scss" scoped>
::v-deep.el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 336px;
}

// .aui-content .ql-container {
//     height: auto;
// }

.listItem,
.listItem2 {
  display: grid;
  margin-bottom: 10px;
  gap: 0 20px;
  grid-template-columns: 300px 200px 200px 40px;
}

.listItem2 {
  grid-template-columns: 200px 200px auto 40px;
}

::v-deep.avatar-uploader .el-upload {
  border: 0;
}

::v-deep.el-time-spinner__wrapper {
  width: 50% !important;

  &:last-child {
    display: none;
  }
}
</style>
